﻿using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Windows.Forms;
using System.Drawing;
using Fluid.Drawing.GdiPlus;
using PasswordSafe.Classes;
using PasswordSafe.Properties;

namespace PasswordSafe
{
    public class ButtonPanel : FluidPanel
    {

        public const int ButtonWidth = 50;
        public const int ButtonHeight = 36;
        const int ButtonTop = 8;
        const int Space = 8;

        protected override void InitControl()
        {
            Theme theme = Theme.Current;
            base.InitControl();
            Bounds = new System.Drawing.Rectangle(0, 0, 240, 40);
            BackColor = theme.ButtonsBackColor;
            GradientFill = theme.ButtonsGradianted;
            GradientFillOffset = 25;

            buttons[0] = btn1 = new FluidButton("", Space, ButtonTop, ButtonWidth, ButtonHeight);
            buttons[1] = btn2 = new FluidButton("", Space + (Space + ButtonWidth) * 1, ButtonTop, ButtonWidth, ButtonHeight);
            buttons[2] = btn3 = new FluidButton("", Space + (Space + ButtonWidth) * 2, ButtonTop, ButtonWidth, ButtonHeight);
            buttons[3] = btn4 = new FluidButton("", Space + (Space + ButtonWidth) * 3, ButtonTop, ButtonWidth, ButtonHeight);


           

            buttons[0].Image = Resources.searchd;
            buttons[1].Image = Resources.undo24_d;
            buttons[2].Image = Resources.saved;
            buttons[3].Image = Resources.Lock;

            

            btn1.Command = "A";
            btn2.Command = "B";
            btn3.Command = "C";
            btn4.Command = "D";

            Color backColor = theme.ButtonsButtonBackColor;

            foreach (FluidButton b in buttons)
            {
                b.Anchor = AnchorStyles.None;
                b.Shape = ButtonShape.Flat;
                b.BackColor = backColor;
                b.ForeColor = Color.White;
            }

            Controls.Add(btn1);
            Controls.Add(btn2);
            Controls.Add(btn3);
            Controls.Add(btn4);

            ListBuilder.Instance.ModifiedChanged += new EventHandler(ListModifiedChanged);

            MakeButtonsGlowing();

        }

        private void MakeButtonsGlowing()
        {
            foreach (FluidButton btn in buttons) btn.PressedBackColor = Color.DarkBlue;
        }

        void ListModifiedChanged(object sender, EventArgs e)
        {
            btn3.Image = ListBuilder.Instance.Modified ? Resources.save : Resources.saved;
            btn2.Image = ListBuilder.Instance.Modified ? Resources.undo24 : Resources.undo24_d;
        }

        FluidButton[] buttons = new FluidButton[4];

        public FluidButton[] Buttons { get { return buttons; } }

        FluidButton btn1;
        FluidButton btn2;
        FluidButton btn3;
        FluidButton btn4;


        protected override void OnSizeChanged(System.Drawing.Size oldSize, System.Drawing.Size newSize)
        {
            base.OnSizeChanged(oldSize, newSize);
            if (btn1 == null) return;

            int ButtonWidth = ScaleX(ButtonPanel.ButtonWidth);
            int ButtonHeight = ScaleY(ButtonPanel.ButtonHeight);
            int ButtonTop = ScaleY(ButtonPanel.ButtonTop);

            if (Width > Height)
            {
                int Space = ScaleX(ButtonPanel.Space);
                btn1.Bounds = new Rectangle(Space, ButtonTop, ButtonWidth, ButtonHeight);
                btn2.Bounds = new Rectangle(Space + (Space + ButtonWidth) * 1, ButtonTop, ButtonWidth, ButtonHeight);
                btn3.Bounds = new Rectangle(Space + (Space + ButtonWidth) * 2, ButtonTop, ButtonWidth, ButtonHeight);
                btn4.Bounds = new Rectangle(Space + (Space + ButtonWidth) * 3, ButtonTop, ButtonWidth, ButtonHeight);
            }
            else
            {
                int Space = ScaleY(ButtonPanel.Space);
                btn1.Bounds = new Rectangle(Space, ButtonTop, ButtonWidth, ButtonHeight);
                btn2.Bounds = new Rectangle(Space, ButtonTop + (ButtonTop + ButtonHeight) * 1, ButtonWidth, ButtonHeight);
                btn3.Bounds = new Rectangle(Space, ButtonTop + (ButtonTop + ButtonHeight) * 2, ButtonWidth, ButtonHeight);
                btn4.Bounds = new Rectangle(Space, ButtonTop + (ButtonTop + ButtonHeight) * 3, ButtonWidth, ButtonHeight);
            }
        }

        private System.Drawing.Rectangle Rectangle(int p, int ButtonTop, int ButtonWidth, int ButtonHeight)
        {
            throw new NotImplementedException();
        }
    }
}
